<template>
  <!-- <div class="container" style="border: red solid 1px; height: 100%">
    <button @click="clear">清空</button>
    <div ref="terminalContainer" class="terminalContainer"></div>
  </div> -->
  <el-container>
    <el-header class="console-clear">
      <el-button @click="clear">清空</el-button>
    </el-header>
    <el-main class="console-main">
      <div ref="terminalContainer" class="terminalContainer"></div>
    </el-main>
  </el-container>
<!--  <div ref="terminalContainer" style="height:200px; ">-->
<!--  <el-button @click="putdata"></el-button>-->
<!--    <ul class="messages">-->
<!--      <li class="message" v-for="n in termInfo">{{ n }}</li>-->
<!--    </ul>-->
<!--  <div ref="testContainer" style="height:200px; border:solid 1px green;overflow-x: auto">-->
<!--  <el-button @click="putdata"></el-button>-->
<!--    <ul>-->
<!--      <li v-for="n in termInfo">{{ n }}</li>-->
<!--    </ul>-->
<!--  </div>-->

</div>


</template>

<script>
import { Terminal } from 'xterm'
import { FitAddon } from 'xterm-addon-fit'
import { WebLinksAddon } from 'xterm-addon-web-links'


import 'xterm/css/xterm.css'
import Pagination from "@/components/Pagination/index";

export default {
  name: 'Console',
  props: {},
  data() {
    return {
      count:0,
      term: null,
      termInfo:["dd","dde","dsafsd",'dddd','ddd3','ddddd']
    }
  },
  methods: {
    putdata(){
      this.termInfo.push("aaa")
      console.log("######",this.termInfo)
      // this.$nextTick(() => {
      //   let container = this.$el.querySelector("#terminalContainer");
      //   console.log(container);
      //   console.log(container.scrollHeight);
      //   container.scrollTop = container.scrollHeight;
      //   console.log(container.scrollTop);
      // })
    },
    scrollToBottom() {
      this.$nextTick(() =>{
        this.$refs.testContainer.scrollTop = this.$refs.testContainer.scrollHeight +10;
        console.log("############",this.$refs.testContainer.scrollHeight)
        console.log("############",this.$refs.testContainer.scrollTop)
      })
    },
    // load () {
    //   // this.termInfo.push("aaa")
    //   // console.log("######",this.termInfo)
    //   this.count +=1;
    // },
    // 清空console
    clear() {
      this.term.clear()
    },
    // 写入消息
    write(str) {
      // this.term.writeln(str)
    },
    // 带时间戳写入消息
    writeWithTime(str) {
      this.term.write(this.getLocalTime())
      this.term.writeln(str)
    },
    // 对小于10的值补零
    addZero(v) {
      return v < 10 ? '0' + v : v
    },
    getLocalTime() {
      const datetime = new Date()
      return `${datetime.getFullYear()}-${this.addZero(
        datetime.getMonth() + 1
      )}-${this.addZero(datetime.getDate())} ${this.addZero(
        datetime.getHours()
      )}:${this.addZero(datetime.getMinutes())}:${this.addZero(
        datetime.getSeconds()
      )} `
    },

  },
  mounted() {
    window.addEventListener("scroll", this.handleScroll, true);
    // console.log('console create')
    // this.term = new Terminal({
    //   // 字体大小
    //   fontSize: 14,
    //   // 游标闪烁
    //   cursorBlink: true,
    //   rows: 40,
    //   theme: {
    //     background: '#c0c0c0',
    //     foreground: '#000000'
    //     // 选中文本颜色
    //     // selection: ''
    //   }
    // })
    // this.fitAddon = new FitAddon()
    // this.term.loadAddon(this.fitAddon)
    // this.term.loadAddon(new WebLinksAddon())
    // // this.term.open(this.$refs.terminalContainer)
    // // 不知道为什么没有生效
    // this.fitAddon.fit()
    // this.term.focus()
    // // this.writeWithTime('dflakjf;lkdjka l;fajkldf j;alsjf')
    // // this.writeWithTime('\x1b[1;31m' + 'User:' + '\x1b[37m' + 'my name\x1b[0m')
    // this.writeWithTime('http://www.baidu.com')
    // console.log(this.writeWithTime('http://www.baidu.com'))
    // this.scrollToBottom()
  },
  // updated: function(){
  //   document.getElementById('terminalContainer').scrollTop = document.getElementById('terminalContainer').scrollHeight;
  // },


  updated(){
    // this.scrollToBottom()
  },

  beforeDestroy() {
    if (this.term) {
      this.term.dispose()
    }
  }
}
</script>

<style scoped>
  .el-carousel__item h3 {
    color: #475669;
    font-size: 14px;
    opacity: 0.75;
    line-height: 20px;
    margin: 0;
  }

  .el-carousel__item:nth-child(2n) {
    background-color: #99a9bf;
  }

  .el-carousel__item:nth-child(2n+1) {
    background-color: #d3dce6;
  }
.console-clear {
  background-color: #b3c0d1;
  color: #333;
  text-align: center;
  line-height: 60px;
}
.console-main {
  background-color: #e9eef3;
  color: #333;
  text-align: center;
  line-height: 160px;
  height: 200px;
}
body > .el-container {
  margin-bottom: 40px;
}
</style>
